package cn.edu.xmu.oomall.liquidation.model.bo.liquid.expenditure;

import cn.edu.xmu.oomall.liquidation.dao.LiquidationDao;
import cn.edu.xmu.oomall.liquidation.microservice.CustomerService;
import cn.edu.xmu.oomall.liquidation.microservice.OrderService;
import cn.edu.xmu.oomall.liquidation.microservice.vo.RefundRetVo;
import cn.edu.xmu.oomall.liquidation.microservice.vo.SimpleOrderRetVo;
import cn.edu.xmu.oomall.liquidation.model.bo.ExpenditureItem;
import cn.edu.xmu.oomall.liquidation.model.bo.Liquidation;
import cn.edu.xmu.oomall.liquidation.model.bo.RevenueItem;
import cn.edu.xmu.oomall.liquidation.model.bo.liquid.calc.CalcTotal;
import cn.edu.xmu.oomall.liquidation.model.po.RevenueItemPoExample;
import cn.edu.xmu.privilegegateway.annotation.util.InternalReturnObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class OrderRefundLiquid extends BaseRefundLiquid{
    @Autowired
    private OrderService orderService;

    @Autowired
    private CustomerService customerService;

    @Override
    protected CalcTotal liquidateSingleRefund(RefundRetVo refund, LocalDateTime liquidDateTime) {
        CalcTotal platformCalcTotal = new CalcTotal(liquidUtil.getPlatformCalcFieldList());
        // 获取父订单Id
        SimpleOrderRetVo parentOrder = orderService.getOrderBySn(refund.getDocumentId()).getData();
        Long parentOrderId = parentOrder.getId();

        // 用父订单Id获得所有的子订单
        InternalReturnObject orderByPid = orderService.getOrderByPid(parentOrderId);
        List<SimpleOrderRetVo> childOrder = (List<SimpleOrderRetVo>) orderByPid.getData();

        for (SimpleOrderRetVo order : childOrder) {
            // 生成清算单统计类,用于更新清算单
            Long shopId = order.getShopId();
            Liquidation liquid = liquidUtil.getLiquid(shopId, liquidDateTime);
            Long liquidId = liquid.getId();

            // 清算子订单
            CalcTotal calcTotal = liquidateChildOrder(order, liquidId, refund);

            //更新清算表
            calcTotal.changeLiquid(liquid);
            liquidUtil.updateLiquid(liquid);

            //更新平台统计
            platformCalcTotal.add(calcTotal);
        }
        return platformCalcTotal;
    }

    /**
     * 订单退款是退整个父订单
     * @param refund
     * @return
     */
    private CalcTotal liquidateChildOrder(SimpleOrderRetVo order, Long liquidId, RefundRetVo refund) {
        CalcTotal calcTotal = new CalcTotal(liquidUtil.getShopCalcFieldList());
        // 通过paymentId和子订单的orderId获取对应的进账单
        RevenueItemPoExample revenueItemPoExample = new RevenueItemPoExample();
        RevenueItemPoExample.Criteria criteria = revenueItemPoExample.createCriteria();
        criteria.andPaymentIdEqualTo(refund.getPaymentId());
        criteria.andOrderIdEqualTo(order.getId());
        List<RevenueItem> revenueItemList = (List<RevenueItem>) liquidUtil.getRevenueItem(revenueItemPoExample).getData();
        //Todo 是否要清算快递费存疑
        for(RevenueItem revenueItem : revenueItemList) {
            ExpenditureItem expenditureItem = new ExpenditureItem();
            expenditureItem.setLiquidId(liquidId);
            expenditureItem.setRefundId(refund.getId());
            copyFromRevenue(expenditureItem, revenueItem);

            // 清算子订单
            Map<String, Long> calcMap = calcFromRevenue(expenditureItem, revenueItem);
            calcTotal.add(calcMap);

            liquidUtil.insertExpenditure(expenditureItem);
        }
        return calcTotal;
    }

    private void copyFromRevenue(ExpenditureItem expenditureItem, RevenueItem revenueItem) {
        expenditureItem.setShopId(revenueItem.getShopId());
        expenditureItem.setRevenueId(revenueItem.getId());
        expenditureItem.setOrderId(revenueItem.getOrderId());
        expenditureItem.setOrderitemId(revenueItem.getOrderitemId());
        expenditureItem.setProductId(revenueItem.getProductId());
        expenditureItem.setProductName(revenueItem.getProductName());
        expenditureItem.setQuantity(revenueItem.getQuantity());
        expenditureItem.setSharerId(revenueItem.getSharerId());
    }

    private Map<String, Long> calcFromRevenue(ExpenditureItem expenditureItem, RevenueItem revenueItem) {
        Map<String, Long> calcMap = new HashMap<>();
        //Todo 从退款单拿 还是从进账单拿
        expenditureItem.setAmount(-revenueItem.getAmount());
        expenditureItem.setExpressFee(-revenueItem.getExpressFee());
        expenditureItem.setCommission(-revenueItem.getCommission());
        expenditureItem.setPoint(-revenueItem.getPoint());
        expenditureItem.setShopRevenue(-revenueItem.getShopRevenue());

        // 扣除分享者返点
        customerService.addCustomerPoint(revenueItem.getSharerId(), expenditureItem.getPoint());

        calcMap.put("expressFee", expenditureItem.getExpressFee());
        calcMap.put("commission", expenditureItem.getCommission());
        calcMap.put("point", expenditureItem.getPoint());
        calcMap.put("shopRevenue", expenditureItem.getShopRevenue());
        return calcMap;
    }
}
